<?php
/**
 * 房源补漏
 */

namespace app\admin\model;


use app\common\model\BaseModel;

class HouseTraps extends BaseModel {

    public static function getList($params) {
        $map = [];
        if (!empty($params['status'])) {
            if ($params['status'] == 1) {
                $map[] = ['ht.status', '=', 0];
            } else {
                $map[] = ['ht.status', '=', 1];
            }
        }
        if (!empty($params['type'])) {
            $map[] = ['ht.type', '=', intval($params['type'])];
        }
        if (!empty($params['keyword'])) {
            $map[] = ['name', 'like', '%' . trim($params['keyword']) . '%'];
        }
        $count = db('house_traps')
            ->alias('ht')
            ->leftjoin('company c', 'c.id = ht.company_id')
            ->where($map)
            ->count();
        if ($count == 0) {
            $result = [
                'page'    => 1,
                'pageNum' => 1,
                'count'   => 0,
                'data'    => [],
            ];
            return $result;
        } else {
            $data = db('house_traps')
                ->alias('ht')
                ->leftjoin('company c', 'c.id = ht.company_id')
                ->where($map)
                ->page($params['page'], $params['long'])
                ->field('c.name,ht.source_url,ht.type,ht.id,ht.itime,ht.status')
                ->order('ht.status asc')
                ->order('ht.id desc')
                ->select();
            $pageNum = ceil($count / $params['long']);
            foreach ($data as $k => $v) {
                $data[$k]['itime'] = date('Y-m-d H:i:s', $v['itime']);
                $data[$k]['name'] = $data[$k]['name'] ?? '';
                $data[$k]['source_url'] = $data[$k]['source_url'] ?? '';

                $sourceList = House::$whereList['source_type'];
                foreach ($sourceList as $arr => $item)
                    $sourceList[$arr]['key'] == $data[$k]['type'] && $data[$k]['type'] = $sourceList[$arr]['value'];
            }
            $result = [
                'page'    => (int)$params['page'],
                'pageNum' => $pageNum,
                'count'   => $count,
                'data'    => $data,
            ];
            return $result;
        }
    }

    /**
     * 处理
     * @param $params
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public static function deal($params) {
        db('house_traps')->where('id', 'in', $params['id'])->update(['status' => 1]);
    }

}